﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using System.Configuration;
using System.IO;

namespace TireService.Website.Reports.View
{
    public partial class Faktura : System.Web.UI.Page
    {

        #region Public Properties

        public int CustomerId
        {
            get
            {
                if (ViewState["_customerid"] != null)
                    return (int)ViewState["_customerid"];
                else
                    return (int)Session["CustomerId"];
            }
            set
            {
                ViewState["_customerid"] = value;
            }
        }

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            //// Load report
            //this.ReportViewer1.Report = new Reports.Design.Invoice();

            //try
            //{
            //    Guid InvoiceGuid = new Guid(Request.QueryString["InvoiceGuid"].ToString());

            //    TireServiceDB db = new TireServiceDB();

            //    Invoice invoice = db.GetInvoice(InvoiceGuid);

            //    if (invoice != null)
            //    {
            //        // Load report
            //        this.ReportViewer1.Report = new Reports.Design.Invoice(InvoiceGuid);    
            //    }

            //    db.Dispose();
                
            //}
            //catch (Exception ex)
            //{
            //    string emailaddress = Convert.ToString(ConfigurationManager.AppSettings["EmailAddress"]);
            //    bool test = Convert.ToBoolean(Convert.ToByte(ConfigurationManager.AppSettings["TestEnvironmentOn"]));
            //    string message = String.Format("Faktura kunne ikke vises pga.: {0}", ex.Message);

            //    Shared email = new Shared();
            //    email.SendEmail(emailaddress, emailaddress, message, "Fejl - faktura visning", true, test, null, null, null);

            //}

            // Input CustomerId and InvoiceId
            try
            {
                int CustomerId = Convert.ToInt32(Request.QueryString["CustomerId"]);

                if (CustomerId != 0)
                {
                    this.CustomerId = CustomerId;    
                }
            }
            catch (Exception)
            {
                
            }
            
            int InvoiceId = Convert.ToInt32(Request.QueryString["InvoiceId"]);

            EconomicInterface ecoInterface = new EconomicInterface();

            //Context.Response.Buffer = false;
            
            //Stream file = ecoInterface.GetInvoiceStream(InvoiceId, this.CustomerId);
            //byte[] mybuff = new byte[1024];
            //long count;
    

            //while ((count = file.Read(mybuff, 0, mybuff.Length)) > 0)
            //{
            //    if (Context.Response.IsClientConnected)
            //    {
            //        Context.Response.OutputStream.Write(mybuff, 0, mybuff.Length);
            //        Context.Response.Flush();
            //    }
            //}



            Context.Response.Buffer = true;
            Context.Response.ClearHeaders();
            Context.Response.ClearContent();
            Context.Response.ContentType = "application/pdf";
            Context.Response.AppendHeader("Content-Disposition", "attachment;filename=Faktura" + InvoiceId + ".pdf");
            Stream input = ecoInterface.GetInvoiceStream(InvoiceId, this.CustomerId);
            Stream output = Context.Response.OutputStream;
            const int Size = 4096;
            byte[] bytes = new byte[4096];
            int totalBytes = 0;
            int numBytes = input.Read(bytes, 0, Size);
            totalBytes += numBytes;
            while (numBytes > 0)
            {
                output.Write(bytes, 0, numBytes);
                numBytes = input.Read(bytes, 0, Size);
                totalBytes += numBytes;
            }

            // You can set this header here thanks to the Response.Buffer = true above
            // This header fixes the Google Chrome bug
            this.Page.Response.AddHeader("Content-Length", totalBytes.ToString());

            input.Close();
            input.Dispose();
            Context.Response.Flush();
            Context.Response.Close();

        }
    }
}